<?php

/*
 * Check if a session user id exist or not. If not set redirect to login page. If the user session id exist and there's found $_GET['logout'] in the query string logout the user
 */
function checkAdminUser() {
	// if the session id is not set, redirect to login page
	if (! isset ( $_SESSION ['username'] ) || ($_SESSION ['usertype'] != 1)) {
		if (isset($_SESSION ['usertype']) && $_SESSION ['usertype'] == 4) { // if normal administrator
			notAllowToAccess(); // register module that not allow normal administrator not access
		} else {
			redirect_to_parent ( W_ROOT . '/admin/login.php' ); // or redirect_to_parent(W_ROOT . '/admin/login.php');
			exit ();
		}
	}

	// the user want to logout
	if (isset ( $_GET ['logout'] )) {
		doLogout ();
	}
}

function notAllowToAccess () {
	$url = $_SERVER['REQUEST_URI'];
	if ((strpos($url, 'user') != FALSE) || (strpos($url, 'account_type') != FALSE)) { // if normal administrator not allow to access to user module
		redirect_to_parent ( W_ROOT . '/404.html' ); // redirect to error page (W_ROOT . '/404.html');
		exit ();
	}
}

function checkAccountType() {
	if ($_SESSION ['acc_type'] == 0) {
		redirect_to_parent ( W_ROOT . '/seller/selectAccountType.php' ); // or redirect_to_parent(W_ROOT . '/admin/login.php');
		exit ();
	}
}
function checkSellerUser() {
	// if the session id is not set, redirect to login page
	if (! isset ( $_SESSION ['username'] ) || $_SESSION ['usertype'] != 2) {
		redirect_to_parent ( W_ROOT . '/login.php' ); // or redirect_to_parent(W_ROOT . '/admin/login.php');
		exit ();
	}

	if (! checkOnlineStatus ( $_SESSION ['acc_id'], $_SESSION ['username'] )) {
		doUserLogout ();
	}

	if (isset ( $_SESSION ['perm'] ) && $_SESSION ['perm'] != 1 && $_SESSION ['perm'] != 2) {
		doUserLogout ();
	}
	// the user want to logout
	if (isset ( $_GET ['dologout'] )) {
		doUserLogout ();
	}
}
function checkBuyerUser() {
	// if the session id is not set, redirect to login page
	if (! isset ( $_SESSION ['username'] ) || $_SESSION ['usertype'] != 3) {
		redirect_to_parent ( W_ROOT . '/login.php' ); // or redirect_to_parent(W_ROOT . '/admin/login.php');
		exit ();
	}
	if (! checkOnlineStatus ( $_SESSION ['acc_id'], $_SESSION ['username'] )) {
		doUserLogout ();
	}

	if (isset ( $_SESSION ['perm'] ) && $_SESSION ['perm'] != 1 && $_SESSION ['perm'] != 2) {
		doUserLogout ();
	}
	// the user want to logout
	if (isset ( $_GET ['do'] ) == "logout") {
		doUserLogout ();
	}
}
function checkOnlineStatus($accId, $accName) {
	$online_r = mysql_query ( "SELECT acc_online FROM tbluser WHERE id=$accId AND email='$accName' AND session_id='" . session_id () . "'" );
	if (dbNumRows ( $online_r )) {
		$online_info = mysql_fetch_assoc ( $online_r );
		extract ( $online_info );
		if ($acc_online == 1)
			return true;
		else
			return false;
	} else
		return false;
}

/*
 * Logout a user
 */
function doLogout() {
	if (isset ( $_SESSION ['acc_id'] )) {
		mysql_query ( "UPDATE tbluser SET session_id='' WHERE id=" . $_SESSION ['acc_id'] );
		unset ( $_SESSION ['acc_id'] );
	}

	if (isset ( $_SESSION ['username'] )) {
		mysql_query ( "UPDATE tbluser SET session_id='' WHERE username='" . $_SESSION ['username'] . "'" );
		unset ( $_SESSION ['username'] );
	}
	if (isset ( $_SESSION ['perm'] )) {
		unset ( $_SESSION ['perm'] );
	}
	if (session_destroy ()) {
		header ( 'Location: ' . W_ROOT . '/admin/login.php' );
		exit ();
	}
}
function doUserLogout() {
	if (isset ( $_SESSION ['acc_id'] )) {
		mysql_query ( "UPDATE tbluser SET session_id='' WHERE id=" . $_SESSION ['acc_id'] );
		unset ( $_SESSION ['username'] );
	}

	if (isset ( $_SESSION ['username'] )) {
		mysql_query ( "UPDATE tbluser SET session_id='' WHERE email='" . $_SESSION ['username'] . "'" );
		unset ( $_SESSION ['username'] );
	}

	if (session_destroy ()) {
		header ( 'Location: ' . W_ROOT . '/login.php' );
		exit ();
	}
}

if (isset ( $_SESSION ['username'] )) {
	$userlogin = $_SESSION ['username'];
} else {
	$userlogin = '';
}

// LABEL IN LIST STYLE
function build_tree($parent_id = 0, $level = 0) {
	// run our query. by default starts at top of
	// the tree (with zero depth)
	$result = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "'" );
	// display each child row
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		// indent as necessary and print name
		echo '<li class="popular-category" id="category-' . $row ['parent_id'] . '">';
		echo str_repeat ( '<div>&nbsp;&nbsp;&nbsp;</div>', $level ) . '<label class="selectit"><input type="checkbox" id="in-category-' . $row ['parent_id'] . '" name="post_category[]" value="' . $row ['id'] . '"> ' . $row ['category_name'] . "\n";
		echo '</label></li>';
		// we want to see all the children that belong to this
		// node� so we need to call *this* function again
		build_tree ( $row ['id'], $level + 1 );
	}
}

// end LABEL IN LIST STYLE
// LABEL IN DROP STYLE
function build_tree_drop($parent_id = 0, $level = 0, $seleced = '') {
	if ((isset ( $_GET ['cateSelect'] ) && ( int ) $_GET ['cateSelect'] > 0)) {
		$changeCate = $_GET ['cateSelect'];
		// echo $changeCate;
		$sql2 = "AND id = $changeCate";
		$qstring = "cateSelect=$changeCate";
	} else {
		$changeCate = 0;
		$sql2 = '';
		$qstring = '';
	}

	// run our query. by default starts at top of
	// the tree (with zero depth)
	$result = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "'" );

	// display each child row
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		if ($row ['id'] == $changeCate) {
			$myselected = ' selected="selected"';
		} else {
			$myselected = '';
		}
		// indent as necessary and print name
		$idcat = $row ['id'];
		$myselect = ($seleced == $idcat ? ' selected="selected"' : '');
		echo '<option value="' . $row ['id'] . '" class="level-' . $row ['parent_id'] . '"' . $myselected . $myselect . '>';
		echo str_repeat ( '&#8212;&nbsp;', $level ) . $row ['category_name'] . "\n";
		echo '</option>';
		// we want to see all the children that belong to this
		// node� so we need to call *this* function again
		build_tree_drop ( $row ['id'], $level + 1 );
	}
}
// end LABEL IN DROP STYLE
function build_tree_drop_category($parent_id = 0, $level = 0, $id = 0) {
	if ((isset ( $_GET ['cateSelect'] ) && ( int ) $_GET ['cateSelect'] > 0)) {
		$changeCate = $_GET ['cateSelect'];
		$sql2 = "AND id = $changeCate";
		$qstring = "cateSelect=$changeCate";
	} else {
		$changeCate = 0;
		$sql2 = '';
		$qstring = '';
	}

	// run our query. by default starts at top of
	// the tree (with zero depth)
	$result = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "'" );

	if ($id != 0) {
		// select to get parent id for set selected
		$sqlSelectParent = 'SELECT parent_id from tblcategory WHERE id=' . $id;
		$parentReuslt = mysql_query($sqlSelectParent);
		$parentObject = mysql_fetch_object($parentReuslt);
		$parent_id = $parentObject->parent_id;
	}
	// display each child row
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		if ($row ['id'] == $parent_id) {
			$myselected = 'selected="selected"';
		} else {
			$myselected = '';
		}
		// indent as necessary and print name
		echo '<option ' . $myselected . ' value="' . $row ['id'] . '" class="level-' . $row ['parent_id'] . '">';
		echo str_repeat ( '&#8212;&nbsp;', $level ) . $row ['category_name'] . "\n";
		echo '</option>';
		// we want to see all the children that belong to this
		build_tree_drop ( $row ['id'], $level + 1, $parent_id );
	}
}
function build_tree_drop_category_seting($parent_id = 0, $level = 0, $Category_01 = 0) {
	if ((isset ( $_GET ['cateSelect'] ) && ( int ) $_GET ['cateSelect'] > 0)) {
		$changeCate = $_GET ['cateSelect'];
		// echo $changeCate;
		$sql2 = "AND id = $changeCate";
		$qstring = "cateSelect=$changeCate";
		// echo $sql2;
	} else {
		$changeCate = 0;
		$sql2 = '';
		$qstring = '';
	}

	// run our query. by default starts at top of
	// the tree (with zero depth)
	$result = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "'" );

	// display each child row
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		if ($row ['id'] == $changeCate) {
			$myselected = 'selected="selected"';
		} else {
			$myselected = '';
		}
		// indent as necessary and print name
		$selected = '';
		if ($Category_01 == $row ['id']) {
			$selected = 'selected="selected"';
		}
		echo '<option ' . $selected . 'value="' . $row ['id'] . '" class="level-' . $row ['parent_id'] . '"' . $myselected . '>';
		echo str_repeat ( '&#8212;&nbsp;', $level ) . $row ['category_name'] . "\n";
		echo '</option>';
		build_tree_drop ( $row ['id'], $level + 1 );
	}
}
function build_tree_cat($parent_id = 0, $level = 0) {
	if ((isset ( $_GET ['cateSelect'] ) && ( int ) $_GET ['cateSelect'] > 0)) {
		$changeCate = $_GET ['cateSelect'];
		// echo $changeCate;
		$sql2 = "AND id = $changeCate";
		$qstring = "cateSelect=$changeCate";
		// echo $sql2;
	} else {
		$changeCate = 0;
		$sql2 = '';
		$qstring = '';
	}

	// run our query. by default starts at top of
	// the tree (with zero depth)
	$result = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "'" );

	// display each child row
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		if ($row ['id'] == $changeCate) {
			$myselected = 'selected="selected"';
		} else {
			$myselected = '';
		}
		// indent as necessary and print name
		echo '<option value="' . $row ['id'] . '" class="level-' . $row ['parent_id'] . '"' . $myselected . '>';
		echo str_repeat ( '&nbsp;&nbsp;&nbsp;', $level ) . $row ['category_name'] . "\n";
		echo '</option>';
		// we want to see all the children that belong to this
		// node� so we need to call *this* function again
		build_tree_cate ( $row ['id'], $level + 1 );
	}
}

// end LABEL IN DROP STYLE
// SHOW POST BY LABEL
function post_show_in_balel($parent_id = 0, $level = 0) {
	if ((isset ( $_GET ['cateSelect'] ) && ( int ) $_GET ['cateSelect'] > 0)) {
		$post_checks = mysql_query ( "SELECT parent_id FROM tblcategory WHERE parent_id='" . $_GET ['cateSelect'] . "'" ) or die ( mysql_error () );
		$post_check_id = mysql_fetch_assoc ( $post_checks );
		if ($post_check_id == 0) {
			echo '<tr>';
			echo '<td></td>';
			echo '<td></td>';
			echo '<td>No posts found.</td>';
			echo '<td></td>';
			echo '<td></td>';
			echo '</tr>';
		} else {

			$id = $_GET ['cateSelect'];

			$show_p_by_label = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "'" );
			// SELECT * FROM tblcategory WHERE parent_id='8';
			while ( $row = mysql_fetch_assoc ( $show_p_by_label ) ) {
				echo '<tr>';
				echo '<td>' . $row ['id'] . '</td>';
				echo '<td>' . $row ['id'] . '</td>';
				echo '<td>' . str_repeat ( '&nbsp;&nbsp;&nbsp;', $level ) . $row ['category_name'] . "\n" . '</td>';
				echo '<td class="right"><a class="btn btn btn-primary btn-small" href="?p=update&edit=' . $row ['id'] . '"><i class="icon-pencil icon-white"></i></a> <a class="btn btn-warning btn-small" href="?p=getcode&get=' . $row ['id'] . '"><i class="icon-share-alt icon-white"></i> Get</a></td>';
				echo '<td></td>';
				echo '</tr>';
				post_show_in_balel ( $row ['parent_id'], $level + 1 );
			}
		}
	}
}

// END SHOW POST BY LABEL
// SHOW ALL POST
function post_all_cat() {
	$result = mysql_query ( "SELECT * FROM tblcategory" );
	while ( $row = mysql_fetch_array ( $result ) ) {

		echo '<tr>';
		echo '<td>' . $row ['id'] . '</td>';
		echo '<td>' . $row ['id'] . '</td>';
		echo '<td>' . $row ['category_name'] . '</td>';
		echo '<td class="right"><a class="btn btn btn-primary btn-small" href="?p=update&edit=' . $row ['id'] . '"><i class="icon-pencil icon-white"></i></a> <a class="btn btn-warning btn-small" href="?p=getcode&get=' . $row ['id'] . '"><i class="icon-share-alt icon-white"></i> Get</a></td>';
		echo '<td></td>';
		echo '</tr>';
	}
}

// END SHOW ALL POST
// LABEL IN LIST STYLE
function cat_by_cat($parent_id = 0, $level = 0) {

	// run our query. by default starts at top of
	// the tree (with zero depth)
	$result = mysql_query ( "SELECT * FROM tblcategory WHERE parent_id='" . $parent_id . "' ORDER BY id DESC" );
	// display each child row
	$i = 0;
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		$i ++;
		echo '<tr>';
		echo '<td>' . $row ['id'] . '</td>';
		echo '<td><input type="checkbox" id="' . $i . '" name="id[]" value="' . $row ['id'] . '"/></td>';
		echo '<td>' . str_repeat ( '&#8212;&nbsp;', $level ) . $row ['category_name'] . "</td>\n";
		echo '<td>' . str_repeat ( '&#8212;&nbsp;', $level ) . stateImage ( $row ['id'], $row ['status'] ) . "</td>\n";
		echo '</tr>';
		cat_by_cat ( $row ['id'], $level + 1 );
	}
}

// end LABEL IN LIST STYLE
?>